//
//  XTCellForeveryone.m
//  TTLestening
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XTCellForeveryone.h"
#import "XTCellofCellForeveryone.h"
#import "XCModelForHeader.h"
#import "XCModelForFind.h"
#import "XCModelForEveryone.h"
#import "XCCellForHeader.h"
#import "XCAVMusicPlayer.h"

#define HEIGHT  [UIScreen mainScreen].bounds.size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface XTCellForeveryone ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableivew;

@property (nonatomic,retain)NSMutableArray *arrayForData;

@property (nonatomic, retain)XCModelForHeader *model;

@end

@implementation XTCellForeveryone


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
       // [self handleData];
        [self createTable];
        
    }
    
    return self;

}



- (void)passModel:(NSArray *)array {


  self.model = [array firstObject];
    self.arrayForData = [NSMutableArray array];
    
    XCModelForFind *modelForsong = [array firstObject];
    NSArray *arrayForData = modelForsong.songs;
    
    //NSLog(@"%ld", arrayForData.count);
    
    for (NSDictionary *dic in arrayForData) {
        
        XCModelForEveryone *model = [[XCModelForEveryone alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.arrayForData addObject:model];
        
        
    }
 
    [self.tableivew reloadData];

}


- (void) createTable {

    self.tableivew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableivew];
    
    self.tableivew.delegate =self;
    self.tableivew.dataSource = self;
    
    self.tableivew.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.tableivew.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableivew.scrollEnabled =NO; //设置tableview 不能滚动
   // NSLog(@"asdad");
    
    UINib *nibCell = [UINib nibWithNibName:@"XTCellofCellForeveryone" bundle:[NSBundle mainBundle]];
    [self.tableivew registerNib:nibCell forCellReuseIdentifier:@"poolForEveryone"];

    [self.tableivew registerClass:[XCCellForHeader class] forHeaderFooterViewReuseIdentifier:@"tableViewHeader"];
    
    

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 40 * HEIGHTT;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    
    XCCellForHeader *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"tableViewHeader"];
   // cell.backgroundColor = [UIColor redColor];
    
    [cell passModel:self.model];
    
    return cell;

}




- (void)layoutSubviews {

    [super layoutSubviews];
    
    self.tableivew.frame = self.contentView.frame;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    return self.arrayshuju.count;
    return self.arrayForData.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return HEIGHT / 10;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XTCellofCellForeveryone *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForEveryone"];

    XCModelForEveryone *model = [self.arrayForData objectAtIndex:indexPath.row];
    
    [cell passModel:model number:indexPath.row + 1];
    
    
   // cell.backgroundColor = [UIColor brownColor];
    
    return cell;
}




-(void)moreMusic:(UIButton *)buuton{
    
    NSLog(@"更多音乐");

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
    XCAVMusicPlayer *musicAVPlayer = [XCAVMusicPlayer sharedMusicPlayer];
    
   // XCModelForEveryone *model = [self.arrayForData objectAtIndex:indexPath.row];
  
  //  NSLog(@"%@", [model.urlList firstObject]);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"giveValue" object:nil userInfo:@{@"modelArray": self.arrayForData, @"index":[NSString stringWithFormat:@"%ld", indexPath.row]}];
    [musicAVPlayer passMusicUrl:self.arrayForData index:indexPath.row];
    
 //   [musicAVPlayer passMusicUrl:[[model.urlList firstObject] objectForKey:@"url"]];

}





- (void)awakeFromNib {
    // Initialization code
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
